<?php
class IndexController extends Zend_Controller_Action
{
	private $model;
	private $modeluser;
	public function init(){
		$this->model = new Default_Model_Product();
		$this->modeluser = new Default_Model_User();
		$this->view->product = $this->model->getall();
		$dirfile = $this->_request->getBaseUrl() . "/application/layouts/default";
		$dirlayout = APPLICATION_PATH . "/layouts/default";
		$this->view->imgs = $dirfile . "/images";
		$this->view->headLink()->appendStylesheet($dirfile . '/css/style.css');
		$this->view->headLink()->appendStylesheet($dirfile . '/css/jquery-ui-1.8.18.custom.css');
		$this->view->headLink()->appendStylesheet($dirfile . '/css/movingboxes.css');
		$this->view->headScript()->appendFile($dirfile . '/js/jquery-1.7.2.min.js');
		$this->view->headScript()->appendFile($dirfile . '/jwplayer/jwplayer.js');
		$this->view->headScript()->appendFile($dirfile . '/js/jquery.movingboxes.js');
		//$this->view->headScript()->appendFile($dirfile . '/js/jquery.carousel.min.js');
	//	$this->view->headScript()->appendFile($dirfile . '/js/jquery.nivo.slider.pack.js');
		$this->view->headScript()->appendFile($dirfile . '/js/dropdown.js');
		$this->view->headScript()->appendFile($dirfile . '/js/player.js');
		$this->view->headScript()->appendFile($dirfile . '/js/slider.js');
		$this->view->headScript()->appendFile($dirfile . '/js/setvideo.js');

		$option = array('layoutPath'=>$dirlayout,'layout'=>'index');
		Zend_Layout::startMvc($option);		
	}
	public function indexAction()
	{
		$newvideo = $this->model->getallnew();
		$this->view->newvideo = $newvideo;
	}
	public function loginAction()
	{
		//$viewhelp = new Zend_View_Helper_BaseUrl();
		$db = Zend_Registry::get('connectDB');
		$auth = Zend_Auth::getInstance();
		$authtable = new Zend_Auth_Adapter_DbTable($db);
		$authtable->setTableName('users')->setIdentityColumn('username')->setCredentialColumn('password');
		$user = $this->_request->getParam('username');
		$pass = $this->_request->getParam('pass');
		$authtable->setIdentity($user);
		$authtable->setCredential(md5($pass));
		$result = $auth->authenticate($authtable);
		if ($result->isValid())
		{
			$data = $authtable->getResultRowObject(null,'password');
			$auth->getStorage()->write($data);
			if ($data->id_group == 2 && $data->active == 1)
			{
				$this->_redirect('/user/');
			$update = $this->modeluser->updatestat($data->id_user,1);
			}
			else
				$this->_redirect('/index/');
		}
		else
			$this->_redirect('/index/');
	}
	public function getdetailAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$id = $this->_request->getParam('id');
		//$name = $this->_request->getParam('name');
		//$id_product = $this->model->getidproduct($name);
		$sesion = new Zend_Session_Namespace('detailvideo');
		//$sesion->video = $id_product['id_product'];
		$sesion->video = $id;
		$this->_forward('detail');
	}
	public function detailAction()
	{
		//$name = $this->_request->getParam('name');
		//$id_product = $this->model->getidproduct($name);
		$sesion = new Zend_Session_Namespace('detailvideo');
		$id = $sesion->video;
		$video = $this->model->getallvideo($id);
		$paging = Zend_Paginator::factory($video);
		$paging->setItemCountPerPage(2);
		$paging->setPageRange(2);
		$current = $this->_request->getParam('page',1);
		$paging->setCurrentPageNumber($current);
		$this->view->paging = $paging;
		//Zend_Debug::dump($id);
	}
	public function handlerAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$id = $this->_request->getParam('id');
		$data = $this->model->getId($id);
		$viewcount = $data['viewcount'];
		$viewcount = $viewcount + 1;
		$update = $this->model->viewcount($id, $viewcount);
		echo $data['name_urlvideo'];
	}
	public function registerAction()
	{
		if ($this->_request->isPost())
		{
		$fullname = $this->_request->getParam('name-user');
		$username = $this->_request->getParam('username');
		$pass = $this->_request->getParam('pass');
		$confirmpass = $this->_request->getParam('confirmpass');
		$address = $this->_request->getParam('address');
		$phone = $this->_request->getParam('phone');
		$email = $this->_request->getParam('email');
		$name_folder = 'folder'.$username;
		try
		{
			if ($pass == $confirmpass)
			$insert = $this->modeluser->add($fullname, $username, $pass, $address, $phone, $email, 2,$name_folder);
			$this->_forward('notify');
		}catch (Zend_Db_Exception $e)
		{
			echo "Loi xu ly";
		}
		}
	}
	public function notifyAction()
	{
		
	}
}